{extend name="common/index"}
{block name="content"}
<div class="layui-row">
    <form id="searchForm" class="layui-form layui-form-pane tpl-form-search" method="post" action="" onkeydown="if(event.keyCode=='13'){reloadTable('#searchForm');return false;}">
        <div class="layui-form-item">
            {eq name="'userInfo.user_id'|session" value="1"}
            <div class="layui-inline">
                <label class="layui-form-label">主键id</label>
                <div class="layui-input-inline">
                    <input type="number" name="id" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">表名称</label>
                <div  class="layui-input-inline">
                    <select name="table_name" >
                        <option value="">请选择</option>
                        {volist name="table_name_list" id="vo"}
                        <option value="{$vo}">{$vo}</option>
                        {/volist}
                    </select>
                </div>
            </div>
            {else /}
            <input type="hidden" name="table_name" value="{$table_name|default=''}">
            {/eq}
            <div class="layui-inline">
                <label class="layui-form-label">时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="create_begin" id="create_begin" class="layui-input">
                </div>
                <div class="layui-form-mid">--</div>
                <div class="layui-input-inline">
                    <input type="text" name="create_end" id="create_end" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-normal" data-type="reload" onclick="reloadTable('#searchForm');">搜索</a>
                </div>
            </div>
        </div>
    </form>
</div>
<!--数据列表-->
<div class="layui-btn-group">
    <button class="layui-btn layui-btn-primary layui-btn-sm" onclick="recoverAll(getSelectedIdList(), '{:url("recover")}');">
    <i class="layui-icon layui-icon-delete"></i>批量还原
    </button>
</div>
<table id="trashTable" lay-filter="trashTable"></table>

<script type="text/html" id="operating">
    <div class="layui-btn-group">
        <a class="layui-btn layui-btn-normal layui-btn-xs" href="javascript:;" lay-event="recover">还原</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" href="javascript:;" lay-event="del">彻底删除</a>
    </div>
</script>

{/block}

{block name="script"}
<script type="text/javascript">
    var form = null;
    var table;
    layui.use(['form','laydate', 'table'], function () {
        form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        table = layui.table;

        /*注册日期，执行一个laydate实例*/
        laydate.render({elem: '#create_begin'});
        laydate.render({elem: '#create_end'});

        //表格数据渲染
        tableIns = table.render({
            elem: '#trashTable'
            ,url: '{:url("Trash/getDataList")}'
            ,page: true
            ,limit: table_limit
            ,limits: table_limits
            ,cols: [[
                {type:'checkbox', width: 45},
                {field:'id', title:'主键id', width:80}
                ,{field:'title', title:'标题', width:200, templet: function (data) {
                    var user_id = {:session('userInfo.user_id')};
                    return user_id == 1 ? data.table_name : data.title;
                }}
                ,{field:'content', title:'内容'}
                ,{field:'create_time', title:'删除时间', width:120}
                ,{field:'create_by', title:'操作者', width:100}
                ,{title:'操作', toolbar: '#operating', align:'center', width:120}
            ]]
            ,where: getFormJson('#searchForm')
        });

        //监听工具条
        table.on('tool(trashTable)', function(obj){
            var data = obj.data;
            if (obj.event === 'recover') {
                layer.confirm('确定还原该笔数据吗？', function(index){
                    $.post('{:url("recover")}',{id:data.id}, function (result) {
                        layer.close(index);
                        if (result.code) {
                            layer.msg(result.msg);
                            reloadTable('#searchForm');
                        } else {
                            layer.alert(result.msg, {icon:2});
                        }
                    }, 'json');
                });
            } else if (obj.event === 'del') {
                layer.confirm('确定彻底删除吗？', function(index){
                    $.post('{:url("delete")}',{id:data.id}, function (result) {
                        layer.close(index);
                        if (result.code) {
                            reloadTable('#searchForm');
                        } else {
                            layer.alert(result.msg, {icon:2});
                        }
                    }, 'json');
                });
            }
        });
    });

    //获取当前选中的id，数组
    function getSelectedIdList() {
        var checkList = table.checkStatus('trashTable');
        var id_list = [];
        if (checkList.data.length>0) {
            $.each(checkList.data, function (key, val) {
                id_list[key] = val.id;
            });
        }
        return id_list;
    }

    //ajax删除
    function recoverAll(id, url) {
        layer.confirm('确定继续吗？', function (index) {
            $.post(url, {id: id}, function (result) {
                layer.close(index);
                if (result.code) {
                    reloadTable('#searchForm');
                    layer.msg(result.msg);
                } else {
                    layer.alert(result.msg, {icon: 2});
                }
            }, 'json');
        });
    }

</script>
{/block}